from django.db import models


class UserInfo(models.Model):
    """用户表"""
    username = models.CharField(max_length=32, verbose_name="用户名")
    password = models.CharField(max_length=32, verbose_name="密码")
    email = models.EmailField(verbose_name="邮箱")

    roles = models.ManyToManyField(to="Role", verbose_name="拥有的角色")

    class Meta:
        verbose_name_plural = "用户表"

    def __str__(self):
        return self.username


class Menu(models.Model):
    """菜单表"""
    title = models.CharField(max_length=32, verbose_name="菜单", unique=True)
    icon = models.CharField(max_length=32, verbose_name="菜单图标")

    class Meta:
        verbose_name_plural = "菜单表"

    def __str__(self):
        return self.title


class Role(models.Model):
    """角色表"""
    title = models.CharField(max_length=32, verbose_name="角色名称")

    permissions = models.ManyToManyField(to="Permission", verbose_name="拥有的权限")

    class Meta:
        verbose_name_plural = "角色表"

    def __str__(self):
        return self.title


class Permission(models.Model):
    """权限表"""
    title = models.CharField(max_length=32, verbose_name="标题")
    url = models.CharField(max_length=128, verbose_name="含则的URL")
    name = models.CharField(max_length=32, verbose_name="别名", null=True, blank=True)

    parent = models.ForeignKey(to="Permission", verbose_name="父权限", null=True, blank=True)
    menu = models.ForeignKey(to="Menu", verbose_name="菜单", null=True, blank=True)

    class Meta:
        verbose_name_plural = "权限表"

    def __str__(self):
        return self.title
